package org.longteng.mobileluck.algorithm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.longteng.core.utils.ChineseYijingNumUtil;
import org.longteng.mobileluck.algorithm.bean.MobileNumAuspiciousTable;
import org.longteng.mobileluck.algorithm.bean.MobileNumAuspiciousVo;
import org.longteng.mobileluck.algorithm.bean.MobileNumFortuneTable;
import org.longteng.mobileluck.algorithm.dao.MobileNumAuspiciousDao;
import org.longteng.mobileluck.algorithm.service.MobileNumAuspiciousService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 手机号码吉祥如意计算service实现
 * @author hymn.com
 */
@Slf4j
@Service
public class MobileNumAuspiciousServiceImpl implements MobileNumAuspiciousService {

    private MobileNumAuspiciousDao mobileNumAuspiciousDao;
    @Autowired
    @Qualifier(value="mobileNumAuspiciousTable")
    public void setMobileNumAuspiciousDao(MobileNumAuspiciousDao mobileNumAuspiciousDao) {
        this.mobileNumAuspiciousDao = mobileNumAuspiciousDao;
    }
    private MobileNumAuspiciousDao mobileNumFortuneDao;
    @Autowired
    @Qualifier(value="mobileNumFortuneTable")
    public void setMobileNumFortuneDao(MobileNumAuspiciousDao mobileNumFortuneDao) {
        this.mobileNumFortuneDao = mobileNumFortuneDao;
    }

    /**
     * 根据完整手机号计算出解释批注测吉凶
     *
     * @param mobilePhoneNum
     * @return
     */
    @Override
    public MobileNumAuspiciousVo parseMobileNumLuck(String mobilePhoneNum) {
        int mobileLuck = this.mobileNumberCalculate(mobilePhoneNum); // 根据中华五格81计算手机号数理
        // 跟据手机号数理得出解释批注测吉凶
        MobileNumAuspiciousTable mobileNumAuspiciousTable = mobileNumAuspiciousDao.getAuspiciousEntity(mobileLuck);
        if (mobileNumAuspiciousTable != null) {
            mobileNumAuspiciousTable.setMobilePhoneNumber(mobilePhoneNum);
        } else {
            log.warn("根据手机号{}数理{}，未找到对应解释批注。", mobilePhoneNum, mobileLuck);
        }

        // 跟据手机号数理得出吉祥运势测吉凶
        MobileNumFortuneTable mobileNumFortuneTable = (MobileNumFortuneTable) mobileNumFortuneDao.getAuspiciousEntity(mobileLuck);
        if (mobileNumFortuneTable != null) {
            mobileNumFortuneTable.setMobilePhoneNumber(mobilePhoneNum);
        } else {
            log.warn("根据手机号{}数理{}，未找到对应吉祥运势。", mobilePhoneNum, mobileLuck);
        }

        MobileNumAuspiciousVo mobileNumAuspiciousVo = new MobileNumAuspiciousVo(mobileNumAuspiciousTable, mobileNumFortuneTable);
        return mobileNumAuspiciousVo;
    }

    /**
     * 根据完整手机号集合计算出解释批注测吉凶
     *
     * @param mobilePhoneNum
     * @return
     */
    @Override
    public List<MobileNumAuspiciousVo> parseMobileNumsLuck(String... mobilePhoneNum) {
        List<MobileNumAuspiciousVo> list = new ArrayList<>();
        for (String mobilePhoneNo : mobilePhoneNum) {
            MobileNumAuspiciousVo mobileNumAuspiciousVo = this.parseMobileNumLuck(mobilePhoneNo);
            list.add(mobileNumAuspiciousVo);
        }

        return list;
    }

    /**
     * 根据完整手机号集合计算出解释批注测吉凶
     *
     * @param mobilePhoneNumList
     * @return
     */
    @Override
    public List<MobileNumAuspiciousVo> parseMobileNumsLuck(List<String> mobilePhoneNumList) {
        List<MobileNumAuspiciousVo> list = new ArrayList<>();
        if (mobilePhoneNumList != null) {
            list = this.parseMobileNumsLuck(mobilePhoneNumList.toArray(new String[mobilePhoneNumList.size()]));
        }
        return list;
    }

    /**
     * 根据中华五格81计算手机号数理
     * <br>公式为：(手机号码最后四位÷80-商的整数)×80=K
     * @param mobilePhoneNum
     * @return
     */
    public int mobileNumberCalculate(String mobilePhoneNum) {
        if(mobilePhoneNum != null) {
            return ChineseYijingNumUtil.yijingNumberCalculate(mobilePhoneNum);
        }
        // 正面的代码可以不用，已写在公共类中

        int result = 0;
        String mobileR4 = StringUtils.right(mobilePhoneNum,4);
        BigDecimal mobileR4int = new BigDecimal(mobileR4);
        BigDecimal baseNum80 = new BigDecimal(80);
        // 返回数组中包含两个元素，第一个元素为两数相除的商，第二个元素为余数
        BigDecimal mobileR4Step1 = mobileR4int.divide(baseNum80);
        log.debug("手机号{}后四位{}除80为：{},参考N/80={}", mobilePhoneNum,mobileR4int, mobileR4Step1,mobileR4int.floatValue()/baseNum80.floatValue());
        // 整数部分
        BigDecimal integerPart = mobileR4Step1.setScale(0, RoundingMode.DOWN);
        // 小数部分
        BigDecimal fractionalPart = mobileR4Step1.subtract(integerPart);
        // ×80=K
        BigDecimal mobileR4Step2 = fractionalPart.multiply(baseNum80);
        result = mobileR4Step2.intValue();
        log.debug("手机号{}后四位{}商{}-整数部分得{}*80为K：{}",mobilePhoneNum,mobileR4int,mobileR4Step1,fractionalPart,mobileR4Step2);

        return result;
    }
}
